package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.U_article;
import cn.com.api.mapper.ApiGoodsMapper;
import cn.com.api.mapper.ApiU_advertMapper;
import cn.com.api.mapper.ApiU_article_replyMapper;
import cn.com.api.mapper.ApiU_discover_cateMapper;
import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.U_articleMapper;
import cn.com.api.service.ApiU_favoriteService;
import cn.com.api.service.impl.base.U_favoriteServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

/**
 * 
 **/
@Service

public class ApiU_favoriteServiceImpl extends U_favoriteServiceImplBase implements ApiU_favoriteService {
	@Autowired
	private ApiU_favoriteMapper apiU_favoriteMapper;
	@Autowired
	private ApiU_discover_cateMapper apiU_discover_cateMapper;
	@Autowired
	private ApiGoodsMapper apiGoodsMapper;
	@Autowired
	private U_articleMapper u_articleMapper;
	@Autowired
	private ApiU_advertMapper apiU_advertMapper;

	public void deleteU_favoriteByMap(Map param) {
		apiU_favoriteMapper.deleteU_favoriteByMap(param);
	}

	public Integer getFavoriteCount(Map param) {
		return apiU_favoriteMapper.getFavoriteCount(param);
	}

	public List<Map<String, Object>> getMyFavoriteSummary(Integer user_id) {
		return apiU_favoriteMapper.getMyFavoriteSummary(user_id);
	}

	@Override
	public ApiU_favorite getFavoriteByCondition(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return apiU_favoriteMapper.getFavoriteByCondition(map);
	}

	public Map<String, Object> MyFavoriteSummary(String user_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		// 新发现收藏数量
		List<Map<String, Object>> u_discover_info = apiU_discover_cateMapper.findDiscoverFavoriteList(user_id);
		Integer discovercount = 0;
		for (Map<String, Object> map2 : u_discover_info) {
			if (map2.get("TYPE") != null && !map2.get("TYPE").equals("")) {
				discovercount += Integer.valueOf(map2.get("FAVORITE_COUNT").toString());
			}
		}
		Map<String, Object> discovermap = new HashMap<String, Object>();
		discovermap.put("COUNT", discovercount);
		discovermap.put("TYPE", 2);
		discovermap.put("CATE", "新发现");

		// 特卖会
		PageInfo page = new PageInfo();
		List<ApiGoods> apigoodsList = apiGoodsMapper.findGoodsListByFavoriteTypeListPage(user_id, Integer.valueOf(1),
				page);
		Integer goodscount = page.getTotalResult();
		Map<String, Object> goodsmap = new HashMap<String, Object>();
		goodsmap.put("COUNT", goodscount);
		goodsmap.put("TYPE", 1);
		goodsmap.put("CATE", "特卖会");

		// 头条
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user_id", user_id);
		List<U_article> article_list = u_articleMapper.getFavoriteArticlesListPage(param, page);
		Integer articlecount = page.getTotalResult();
		Map<String, Object> articlemap = new HashMap<String, Object>();
		articlemap.put("COUNT", articlecount);
		articlemap.put("TYPE", 5);
		articlemap.put("CATE", "头条");

		// 红包广告
		List<ApiU_advert> advList = this.apiU_advertMapper.findAdvertListByFavoriteTypeListPage(user_id, 6, page);
		Integer advListcount = page.getTotalResult();
		Map<String, Object> advmap = new HashMap<String, Object>();
		advmap.put("COUNT", advListcount);
		advmap.put("TYPE", 4);
		advmap.put("CATE", "红包广告");

		List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
		resultMap.add(articlemap);
		resultMap.add(advmap);
		resultMap.add(goodsmap);
		resultMap.add(discovermap);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作完成");
		map.put(Utils.DATAS, resultMap);
		return map;
	}

}
